package homework.no_3;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * 向服务器发出登录请求；服务器接收数据并进行判断，如果用户名和密码均是bjsxt，则登录成功，
 * 否则登录失败，返回相应响应信息；客户端接收响应信息并输出登录结果。
 */
public class ServerTest {

    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket socket = null;
        ObjectInputStream ois = null;
        DataOutputStream dos = null;


        try {
            ss = new ServerSocket(8800);
            socket = ss.accept();

            ois = new ObjectInputStream(socket.getInputStream());
            dos = new DataOutputStream(socket.getOutputStream());
            User user =(User)ois.readObject();
            boolean flag = false;
            if ("bjsxt".equals(user.getUserName()) && "bjsxt".equals(user.getPwd())){
                flag = true;
            }
            dos.writeBoolean(flag);


        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if ( ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dos != null){
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
